import sqlite3
from hashlib import sha256
import base64

USER_NOW = {"value": None}


#AccessKey = v-wlxYmoUzYxiwLHk_SYgw
#SecretKey = sI8NCh1sDswJpawQL9xMgemzmubVYrX8

#AccessKey = 6-mgjeOJH9RQDAxv4Phlrg
#SecretKey = 2aCs1TFYIzby2Tpoprn9ZtqUtvGgBXn5

# 创建 SQLite 数据库（如果不存在的话）
conn = sqlite3.connect('user_keys.db')
cur = conn.cursor()

# 创建一个表格来存储用户的 AccessKey 和 SecretKey
cur.execute('''
CREATE TABLE IF NOT EXISTS user_keys (
    user_id INTEGER PRIMARY KEY,
    access_key TEXT NOT NULL,
    secret_key TEXT NOT NULL
)
''')

conn.commit()
cur.close()
conn.close()


def store_user_keys(user_id, access_key, secret_key):
    conn = sqlite3.connect('user_keys.db')
    cur = conn.cursor()
    
    cur.execute('''
        INSERT OR REPLACE INTO user_keys (user_id, access_key, secret_key)
        VALUES (?, ?, ?)
    ''', (user_id, access_key, secret_key))
    
    conn.commit()
    cur.close()
    conn.close()

def get_user_keys(user_id):
    conn = sqlite3.connect('user_keys.db')
    cur = conn.cursor()
    cur.execute("SELECT access_key, secret_key FROM user_keys WHERE user_id = ?", (user_id,))
    row = cur.fetchone()
    cur.close()
    conn.close()
    
    if row:
        access_key, secret_key = row
        return access_key, secret_key
    return None, None

# 获取用户总数
def get_user_count():
    conn = sqlite3.connect('user_keys.db')
    cur = conn.cursor()
    cur.execute("SELECT COUNT(*) FROM user_keys")
    count = cur.fetchone()[0]
    cur.close()
    conn.close()
    return count

USER_NOW["value"]=get_user_count()